# -*- coding: utf-8 -*-
"""
******* 文档说明 ******
Python 调用 C/C++ 动态库示例代码

"""
import ctypes

# 导入C动态库 .dll/.so
lib = ctypes.cdll.LoadLibrary(r'.\add.dll')

# lib.main()

res_int = lib.add_int(4, 5)
print("4 + 5 = " + str(res_int))

a = ctypes.c_float(5.5)
b = ctypes.c_float(4.1)

add_float = lib.add_float
add_float.restype = ctypes.c_float

print("5.5 + 4.1 = " + str(add_float(a, b)))
print('- '*30)

# 字符串逆序
lib_reverse = lib.reverse
lib_reverse.restype = ctypes.c_char_p

a_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'*100
aa_str = a_str.encode('utf-8')

import time
start_time = time.time()
for _ in range(10000):
    p = lib_reverse(aa_str)
print('\n调用C Used Time {:.3f} Sec'.format((time.time() - start_time)))
print(lib_reverse(aa_str))

start_time = time.time()
for _ in range(10000):
    pp = list(reversed(a_str))
print('\nPython Used Time {:.3f} Sec'.format((time.time() - start_time)))
print(list(reversed(a_str)))

start_time = time.time()
for _ in range(10000):
    b = a_str[::-1]
print('\nPython Used Time {:.3f} Sec'.format((time.time() - start_time)))
print(b)
